﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO;

namespace MLP
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            LoadConfiguration("config.txt");
            System.Globalization.CultureInfo ci = new System.Globalization.CultureInfo("en-us");
            System.Threading.Thread.CurrentThread.CurrentCulture = ci;

        }

        private void LoadConfiguration(string configFile)
        {
            if (File.Exists(configFile))
            {
                string[] S = File.ReadAllLines(configFile);
                foreach (string s in S)
                {
                    string[] S2 = s.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);
                    if (S2.Length == 2)
                        switch (S2[0])
                        {
                            case "numHiddenNeurons1TextBox": numHiddenNeurons1TextBox.Text = S2[1]; break;
                            case "numHiddenNeurons2TextBox": numHiddenNeurons2TextBox.Text = S2[1]; break;
                            case "numHiddenNeurons3TextBox": numHiddenNeurons3TextBox.Text = S2[1]; break;
                            case "numHiddenNeurons4TextBox": numHiddenNeurons4TextBox.Text = S2[1]; break;
                            case "numHiddenNeurons5TextBox": numHiddenNeurons5TextBox.Text = S2[1]; break;
                            case "numHiddenNeurons6TextBox": numHiddenNeurons6TextBox.Text = S2[1]; break;
                            case "numHiddenNeurons7TextBox": numHiddenNeurons7TextBox.Text = S2[1]; break;
                            case "numHiddenNeurons8TextBox": numHiddenNeurons8TextBox.Text = S2[1]; break;
                            case "numHiddenNeurons9TextBox": numHiddenNeurons9TextBox.Text = S2[1]; break;
                            case "numHiddenNeurons10TextBox": numHiddenNeurons10TextBox.Text = S2[1]; break;
                            case "numHiddenNeurons11TextBox": numHiddenNeurons11TextBox.Text = S2[1]; break;
                            case "numHiddenNeurons12TextBox": numHiddenNeurons12TextBox.Text = S2[1]; break;
                            case "numEpochTextBox": numEpochTextBox.Text = S2[1]; break;
                            case "sumMedianTextBox": sumMedianTextBox.Text = S2[1]; break;
                            case "weightRegularizationTextBox": weightRegularizationTextBox.Text = S2[1]; break;
                            case "percentTestSetTextBox": percentTestSetTextBox.Text = S2[1]; break;
                            case "dwTextBox": dwTextBox.Text = S2[1]; break;
                            case "crossvalidationTextBox": crossvalidationTextBox.Text = S2[1]; break;
                            case "crossvalidationRunsTextBox": crossvalidationRunsTextBox.Text = S2[1]; break;
                            case "TextBoxOutliers": TextBoxOutliers.Text = S2[1]; break;
                            case "TextBoxNoiseValue": TextBoxNoiseValue.Text = S2[1]; break;
                            case "TextBoxNoiseFrequency": TextBoxNoiseFrequency.Text = S2[1]; break;
                            case "TextBoxTheta": TextBoxTheta.Text = S2[1]; break;
                            case "initDir": initDir = S2[1]; break;
                            case "CheckBoxInputNoise": CheckBoxInputNoise.IsChecked = S2[1] == "true" ? true : false; break;
                            case "CheckBoxOutputNoise": CheckBoxOutputNoise.IsChecked = S2[1] == "true" ? true : false; break;
                            case "RadioButtonGAS": RadioButtonGAS.IsChecked = S2[1] == "true" ? true : false; break;
                            case "CheckBoxRandomWeights": CheckBoxRandomWeights.IsChecked = S2[1] == "true" ? true : false; break;
                            case "CheckBoxGraphics1": CheckBoxGraphics1.IsChecked = S2[1] == "true" ? true : false; break;
                            case "CheckBoxGraphics2": CheckBoxGraphics2.IsChecked = S2[1] == "true" ? true : false; break;
                            case "CheckBoxGraphics3": CheckBoxGraphics3.IsChecked = S2[1] == "true" ? true : false; break;
                            case "CheckBoxGraphics4": CheckBoxGraphics4.IsChecked = S2[1] == "true" ? true : false; break;
                            case "CheckBoxSaveLearning1": CheckBoxSaveLearning1.IsChecked = S2[1] == "true" ? true : false; break;
                            case "CheckBoxSaveLearning2": CheckBoxSaveLearning2.IsChecked = S2[1] == "true" ? true : false; break;
                            case "CheckBoxSaveLearning3": CheckBoxSaveLearning3.IsChecked = S2[1] == "true" ? true : false; break;
                            case "CheckBoxSaveLearning4": CheckBoxSaveLearning4.IsChecked = S2[1] == "true" ? true : false; break;
                            case "RadioButtonENN": RadioButtonENN.IsChecked = S2[1] == "true" ? true : false; break;
                            case "ComboBoxTrainingAlgorithm": ComboBoxTrainingAlgorithm.SelectedIndex = Convert.ToInt32(S2[1]); break;
                            case "lastInputFileName": lastInputFileName = S2[1]; CheckBoxLastDataFile.Content = "Auto-load " + S2[1]; break;
                            case "CheckBoxShowTables": CheckBoxShowTables.IsChecked = S2[1] == "true" ? true : false; break;
                        }
                }
            }
        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            SaveConfiguration("config.txt");
        }

        private void SaveConfiguration(string configFile)
        {
            string[] S3 = new string[]
            {       
                 "numHiddenNeurons1TextBox " + numHiddenNeurons1TextBox.Text,
                 "numHiddenNeurons2TextBox " + numHiddenNeurons2TextBox.Text,
                 "numHiddenNeurons3TextBox " + numHiddenNeurons3TextBox.Text,
                 "numHiddenNeurons4TextBox " + numHiddenNeurons4TextBox.Text,
                 "numHiddenNeurons5TextBox " + numHiddenNeurons5TextBox.Text,
                 "numHiddenNeurons6TextBox " + numHiddenNeurons6TextBox.Text,
                 "numHiddenNeurons7TextBox " + numHiddenNeurons7TextBox.Text,
                 "numHiddenNeurons8TextBox " + numHiddenNeurons8TextBox.Text,
                 "numHiddenNeurons9TextBox " + numHiddenNeurons9TextBox.Text,
                 "numHiddenNeurons10TextBox " + numHiddenNeurons10TextBox.Text,
                 "numHiddenNeurons11TextBox " + numHiddenNeurons11TextBox.Text,
                 "numHiddenNeurons12TextBox " + numHiddenNeurons12TextBox.Text,
                 "numEpochTextBox " + numEpochTextBox.Text,
                 "sumMedianTextBox " + sumMedianTextBox.Text,
                 "weightRegularizationTextBox "+ weightRegularizationTextBox.Text,
                 "percentTestSetTextBox "+ percentTestSetTextBox.Text,
                 "dwTextBox " + dwTextBox.Text,
                 "crossvalidationTextBox " + crossvalidationTextBox.Text, 
                 "crossvalidationRunsTextBox " + crossvalidationRunsTextBox.Text, 
                 "TextBoxOutliers " + TextBoxOutliers.Text,
                 "TextBoxNoiseValue " +  TextBoxNoiseValue.Text,
                 "TextBoxNoiseFrequency " +  TextBoxNoiseFrequency.Text,    
                 "TextBoxTheta " +  TextBoxTheta.Text,   
                 "initDir " + initDir,
                 CheckBoxInputNoise.IsChecked == true ? "CheckBoxInputNoise true" : "CheckBoxInputNoise false",
                 CheckBoxOutputNoise.IsChecked == true ? "CheckBoxOutputNoise true" : "CheckBoxOutputNoise false",
                 RadioButtonGAS.IsChecked == true ? "RadioButtonGAS true" : "RadioButtonGAS false",
                 CheckBoxRandomWeights.IsChecked == true ? "CheckBoxRandomWeights true" : "CheckBoxRandomWeights false", 
                 CheckBoxGraphics1.IsChecked == true ? "CheckBoxGraphics1 true" : "CheckBoxGraphics1 false", 
                 CheckBoxGraphics2.IsChecked == true ? "CheckBoxGraphics2 true" : "CheckBoxGraphics2 false", 
                 CheckBoxGraphics3.IsChecked == true ? "CheckBoxGraphics3 true" : "CheckBoxGraphics3 false", 
                 CheckBoxGraphics4.IsChecked == true ? "CheckBoxGraphics4 true" : "CheckBoxGraphics4 false", 
                 CheckBoxSaveLearning1.IsChecked == true ? "CheckBoxSaveLearning1 true" : "CheckBoxSaveLearning1 false", 
                 CheckBoxSaveLearning2.IsChecked == true ? "CheckBoxSaveLearning2 true" : "CheckBoxSaveLearning2 false", 
                 CheckBoxSaveLearning3.IsChecked == true ? "CheckBoxSaveLearning3 true" : "CheckBoxSaveLearning3 false", 
                 CheckBoxSaveLearning4.IsChecked == true ? "CheckBoxSaveLearning4 true" : "CheckBoxSaveLearning4 false", 
                 RadioButtonENN.IsChecked == true ? "RadioButtonENN true" : "RadioButtonENN false",
                 CheckBoxShowTables.IsChecked == true ? "CheckBoxShowTables true" : "CheckBoxShowTables false",
                 "ComboBoxTrainingAlgorithm " + ComboBoxTrainingAlgorithm.SelectedIndex, 
                 "lastInputFileName " + lastInputFileName
            };
            File.WriteAllLines(configFile, S3);
        }




        double[,] DataSet;
        int lastColumnContainsOutliers = 0, tVect = 0, tAttr = 0;
        string outputFileName = "", inputFileName = "", fileName, directoryName, headerLine, initDir, lastInputFileName = "";
        Network[] mlpx = new Network[4];
        bool[] SaveLearning = new bool[4];
        bool trainingSetInAnotherFile = false;
        int[] VSS_version = { 0, 0, 0, 0 };


        private void LoadDataset(object sender, RoutedEventArgs e)
        {

            Microsoft.Win32.OpenFileDialog dlg1 = new Microsoft.Win32.OpenFileDialog();
            dlg1.InitialDirectory = initDir;

            if (dlg1.ShowDialog() == true)
            {
                lastInputFileName = dlg1.FileName;
                LoadTrainingDataSet(lastInputFileName);
                CheckBoxLastDataFile.Content = "Auto-load " + lastInputFileName;
            }
        }

        bool recentlyLoadedDataSet = false;
        private void LoadTrainingDataSet(string inputFileName1)
        {
            DataSet = Utils.LoadFile(inputFileName1, out headerLine);

            lastColumnContainsOutliers = 0;
            if (headerLine.EndsWith("outlier"))
                lastColumnContainsOutliers = 1;
            if (headerLine.Contains("Class") || headerLine.Contains("class"))
                classRB.IsChecked = true;
            else
                regrRB.IsChecked = true;

            string inputFileName2 = System.IO.Path.GetFileName(inputFileName1);
            fileName = inputFileName2.Substring(0, inputFileName2.LastIndexOf("."));
            LabelDataset.Content = fileName.Replace("_", "__") + "    " + (tVect = DataSet.GetLength(0)).ToString() + " vect.   " + (tAttr = DataSet.GetLength(1)).ToString() + " attr.";
            if (headerLine.Contains("Class") || headerLine.Contains("class"))
            {                
                int nv = DataSet.GetLength(0);
                int na = DataSet.GetLength(1)-1;
                if (headerLine.ToLower().Contains("outlier"))
                    na = DataSet.GetLength(1) - 2;
                SortedSet<int> Nout = new SortedSet<int>();
                for (int v = 0; v < nv; v++)
                    Nout.Add((int)DataSet[v, na]);
                
                LabelDataset.Content += "  " + Nout.Count.ToString() + " classes";
            }
            else
                LabelDataset.Content += "   regression";

            trainingSetInAnotherFile = false;

            directoryName = System.IO.Path.GetDirectoryName(lastInputFileName);
            initDir = directoryName;

            if (!Directory.Exists(directoryName + "\\Results"))
                Directory.CreateDirectory(directoryName + "\\Results");

            inputFileName = inputFileName1.Substring(0, inputFileName1.LastIndexOf("."));

            buttonTraining1.IsEnabled = true;
            buttonTraining2.IsEnabled = true;
            buttonTraining3.IsEnabled = true;
            buttonTraining4.IsEnabled = true;
            ContrastiveDivergenceButton.IsEnabled = true;
            ContrastiveDivergenceDiscrButton.IsEnabled = true;
            buttonCrossvalidation.IsEnabled = true;
            recentlyLoadedDataSet = true;
        }


        private void LoadTestDataset(object sender, RoutedEventArgs e)
        {
            recentlyLoadedDataSet = false;
            Microsoft.Win32.OpenFileDialog dlg1 = new Microsoft.Win32.OpenFileDialog();
            dlg1.InitialDirectory = initDir;
            if (dlg1.ShowDialog() == true)
            {

                LoadDataByFileName(dlg1.FileName);

                if (tAttr != DataSet.GetLength(1))
                {
                    MessageBox.Show("Error! Different number of attributes in training and test set.");
                    return;
                }


            }
        }


        void LoadDataByFileName(string inputFileName1)
        {
            DataSet = Utils.LoadFile(inputFileName1, out headerLine);
            lastColumnContainsOutliers = 0;

            string inputFileName2 = System.IO.Path.GetFileName(inputFileName1);
            fileName = inputFileName2.Substring(0, inputFileName2.LastIndexOf("."));

            LabelDataset.Content = fileName.Replace("_", "__") + "    " + DataSet.GetLength(0).ToString() + " vect.   " + (DataSet.GetLength(1) - 1).ToString() + " attr.";
            if (headerLine.Contains("Class") || headerLine.Contains("class"))
            {
                int nv = DataSet.GetLength(0);
                int na = DataSet.GetLength(1) - 1;
                if (headerLine.ToLower().Contains("outlier"))
                    na = DataSet.GetLength(1) - 2;
                SortedSet<int> Nout = new SortedSet<int>();
                for (int v = 0; v < nv; v++)
                    Nout.Add((int)DataSet[v, na]);

                LabelDataset.Content += "  " + Nout.Count.ToString() + " classes";
            }
            else
                LabelDataset.Content += "   regression";
            
            
            
            trainingSetInAnotherFile = true;

            directoryName = System.IO.Path.GetDirectoryName(inputFileName1);
            initDir = directoryName;

            if (!Directory.Exists(directoryName + "\\Results"))
                Directory.CreateDirectory(directoryName + "\\Results");

            inputFileName = inputFileName1.Substring(0, inputFileName1.LastIndexOf("."));
        }

        private async void NetworkTraining1(object sender, RoutedEventArgs e)
        {
            messageTestSetTextBox1.Text = "";
            await NetworkTraining(0, messageTrainingSetTextBox1, CheckBoxSaveLearning1, CheckBoxGraphics1, buttonTest1); 
        }

        private async void NetworkTraining2(object sender, RoutedEventArgs e)
        {
            messageTestSetTextBox2.Text = "";
            await NetworkTraining(1, messageTrainingSetTextBox2, CheckBoxSaveLearning2, CheckBoxGraphics2, buttonTest2);
        }

        private async void NetworkTraining3(object sender, RoutedEventArgs e)
        {
            messageTestSetTextBox3.Text = "";
            await NetworkTraining(2, messageTrainingSetTextBox3, CheckBoxSaveLearning3, CheckBoxGraphics3, buttonTest3);
        }

        private async void NetworkTraining4(object sender, RoutedEventArgs e)
        {
            messageTestSetTextBox4.Text = "";
            await NetworkTraining(3, messageTrainingSetTextBox4, CheckBoxSaveLearning4, CheckBoxGraphics4, buttonTest4);
        }

        private void NetworkTest1(object sender, RoutedEventArgs e)
        {
            NetworkTest(0, messageTestSetTextBox1);
        }

        private void NetworkTest2(object sender, RoutedEventArgs e)
        {
            NetworkTest(1, messageTestSetTextBox2);
        }

        private void NetworkTest3(object sender, RoutedEventArgs e)
        {
            NetworkTest(2, messageTestSetTextBox3);
        }

        private void NetworkTest4(object sender, RoutedEventArgs e)
        {
            NetworkTest(3, messageTestSetTextBox4);
        }


        private async Task NetworkTraining(int n, TextBox messageTrainingSetTextBoxN, CheckBox CheckBoxSaveLearningN,
            CheckBox CheckBoxGraphicsN, Button buttonTestN)
        {
            recentlyLoadedDataSet = false;
            //Cursor c = Mouse.OverrideCursor;                    
            //Mouse.OverrideCursor  = Cursors.Wait;
            SolidColorBrush scb = (SolidColorBrush)(messageTrainingSetTextBoxN.Background);
            messageTrainingSetTextBoxN.Background = new SolidColorBrush(Colors.Red);
            messageTrainingSetTextBoxN.Text = "working";

            if (CheckBoxSaveLearningN.IsChecked == true)
                SaveLearning[n] = true;
            else
                SaveLearning[n] = false;
            
            
            
            if (CheckBoxLastDataFile.IsChecked == true)
                LoadDataByFileName(lastInputFileName);


            string resultDirectory = directoryName + "\\Results\\" + fileName + "\\" + DateTime.Now.Ticks.ToString();
            Directory.CreateDirectory(resultDirectory);
            outputFileName = resultDirectory + "\\" + fileName;
            int numEpoch = Convert.ToInt32(numEpochTextBox.Text);
            double sumMedian = Convert.ToDouble(sumMedianTextBox.Text);
            double weightRegularization = Convert.ToDouble(weightRegularizationTextBox.Text);
            double percentTestSet = Convert.ToDouble(percentTestSetTextBox.Text);
            int transferFunction = transferFunctionComboBox.SelectedIndex;
            int errorMeasure = errorMeasureComboBox.SelectedIndex;          
            double errExp = Convert.ToDouble(errExpTextBox.Text);
            double dw = Convert.ToDouble(dwTextBox.Text);
            bool classRegr = (classRB.IsChecked == true) ? true : false;
            bool linRegr = (CheckBoxLinearRegr.IsChecked == true) ? true : false;
            double outCoef = Convert.ToDouble(TextBoxOutliers.Text);
            bool RandomWeights = (CheckBoxRandomWeights.IsChecked == true) ? true : false;
            double eta = Convert.ToDouble(TextBoxEta.Text);
            double alpha = Convert.ToDouble(TextBoxAlpha.Text);
            double etaRpropPlus = Convert.ToDouble(TextBoxEtaRpropPlus.Text);
            double etaRpropMinus = Convert.ToDouble(TextBoxEtaRpropMinus.Text);
            string trainingAlgorithm = ComboBoxTrainingAlgorithm.SelectionBoxItem.ToString();
            int robustAlgorithm = robustAlgorithmComboBox.SelectedIndex;
            bool productUnits = CheckBoxProductUnits.IsChecked == true;
            bool ENN = RadioButtonENN.IsChecked == true;
            double theta = Convert.ToDouble(TextBoxTheta.Text);
            VSS_version[n] = ComboBoxTrainingAlgorithm.SelectedIndex;
            

            List<int> nHidden = new List<int>();

            if (numHiddenNeurons1TextBox.Text.Trim() != "" && numHiddenNeurons1TextBox.Text.Trim() != "0")
                nHidden.Add(Convert.ToInt32(numHiddenNeurons1TextBox.Text));
            if (numHiddenNeurons2TextBox.Text.Trim() != "" && numHiddenNeurons2TextBox.Text.Trim() != "0")
                nHidden.Add(Convert.ToInt32(numHiddenNeurons2TextBox.Text));
            if (numHiddenNeurons3TextBox.Text.Trim() != "" && numHiddenNeurons3TextBox.Text.Trim() != "0")
                nHidden.Add(Convert.ToInt32(numHiddenNeurons3TextBox.Text));
            if (numHiddenNeurons4TextBox.Text.Trim() != "" && numHiddenNeurons4TextBox.Text.Trim() != "0")
                nHidden.Add(Convert.ToInt32(numHiddenNeurons4TextBox.Text));
            if (numHiddenNeurons5TextBox.Text.Trim() != "" && numHiddenNeurons5TextBox.Text.Trim() != "0")
                nHidden.Add(Convert.ToInt32(numHiddenNeurons5TextBox.Text));
            if (numHiddenNeurons6TextBox.Text.Trim() != "" && numHiddenNeurons6TextBox.Text.Trim() != "0")
                nHidden.Add(Convert.ToInt32(numHiddenNeurons6TextBox.Text));
            if (numHiddenNeurons7TextBox.Text.Trim() != "" && numHiddenNeurons7TextBox.Text.Trim() != "0")
                nHidden.Add(Convert.ToInt32(numHiddenNeurons7TextBox.Text));
            if (numHiddenNeurons8TextBox.Text.Trim() != "" && numHiddenNeurons8TextBox.Text.Trim() != "0")
                nHidden.Add(Convert.ToInt32(numHiddenNeurons8TextBox.Text));
            if (numHiddenNeurons9TextBox.Text.Trim() != "" && numHiddenNeurons9TextBox.Text.Trim() != "0")
                nHidden.Add(Convert.ToInt32(numHiddenNeurons9TextBox.Text));
            if (numHiddenNeurons10TextBox.Text.Trim() != "" && numHiddenNeurons10TextBox.Text.Trim() != "0")
                nHidden.Add(Convert.ToInt32(numHiddenNeurons10TextBox.Text));
            if (numHiddenNeurons11TextBox.Text.Trim() != "" && numHiddenNeurons11TextBox.Text.Trim() != "0")
                nHidden.Add(Convert.ToInt32(numHiddenNeurons11TextBox.Text));
            if (numHiddenNeurons12TextBox.Text.Trim() != "" && numHiddenNeurons12TextBox.Text.Trim() != "0")
                nHidden.Add(Convert.ToInt32(numHiddenNeurons12TextBox.Text));

            int[] numHidden = nHidden.ToArray();

            SaveConfiguration(outputFileName + "_config.txt");
            TimeSpan ts = new TimeSpan(0);
            await Task.Run(
                 () =>
                 {

                     if (ENN)
                     {
                         kNN knn = new kNN(DataSet, 9, 2);
                         knn.GetDistances();
                         knn.ENN(theta);
                         DataSet = Utils.LoadFile(knn.SaveDataSetWithOutliers2(1, resultDirectory + "\\" + "file_ENN.txt", headerLine), out headerLine);
                         GC.Collect();
                     }
                     else if (outCoef > 0)
                     {
                         kNN knn = new kNN(DataSet, 9, 2);
                         knn.GetDistances();
                         knn.ENN(theta);
                         DataSet = Utils.LoadFile(knn.SaveDataSetWithOutliers2(0, resultDirectory + "\\" + "file_outliers.txt", headerLine), out headerLine);
                         GC.Collect();
                         lastColumnContainsOutliers = 1;
                     }


                     mlpx[n] = new Network(DataSet, numHidden, numEpoch, sumMedian, weightRegularization,
                                        percentTestSet, outputFileName, transferFunction, dw, classRegr, linRegr,
                                        outCoef, lastColumnContainsOutliers, RandomWeights, errorMeasure, errExp, VSS_version[n], SaveLearning[n]);
                     //mlpx[n].useSignalTable = useSignalTable; 

                     mlpx[n].productUnits = productUnits;
                     mlpx[n].trainingAlgorithm = trainingAlgorithm;                     


                     if (robustAlgorithm > 0)
                     {
                         if (robustAlgorithm == 1)                         
                             mlpx[n].IM_VSS(VSS_version[n]);                        
                         else 
                             mlpx[n].LT_VSS(VSS_version[n]);
                     }
                     else
                     {
                         if (trainingAlgorithm.Substring(0, 3) == "VSS")
                         {
                             DateTime dt1 = DateTime.Now;            
                             mlpx[n].VSS(VSS_version[n]);
                             DateTime dt2 = DateTime.Now;
                             ts = dt2 - dt1;
                         }
                         else
                             mlpx[n].BP(eta, alpha, etaRpropPlus, etaRpropMinus);
                     }

                     StreamWriter sw0 = new StreamWriter(outputFileName + "_finalWeights.txt");
                     sw0.Write("Network: ");
                     for (int i = 0; i < mlpx[n].numLayers - 1; i++)
                         sw0.Write(mlpx[n].Layer[i] + "-");
                     sw0.WriteLine(mlpx[n].Layer[mlpx[n].numLayers - 1]);

                     for (int L0 = 1; L0 < mlpx[n].numLayers; L0++)
                         for (int n0 = 0; n0 < mlpx[n].Layer[L0]; n0++)
                             for (int w0 = 0; w0 < mlpx[n].Layer[L0 - 1] + 1; w0++)
                                 if (VSS_version[n] > 0)
                                    sw0.WriteLine(mlpx[n].weights[L0, n0, w0]);
                                 else
                                    sw0.WriteLine(mlpx[n].N[L0][n0].weight[w0]);

                     sw0.Close();
                 });



            //  messageTestSetTextBox1.Text = mlpx[n].Layer[0].ToString() + "-" + mlpx[n].Layer[1].ToString() + "-" + mlpx[n].Layer[2].ToString();
            messageTrainingSetTextBoxN.Background = scb;
            messageTrainingSetTextBoxN.Text = "M=" + Math.Round(mlpx[n].error / mlpx[n].TrainingDataSet.GetLength(0), 4).ToString()
                + " R=" + Math.Round(Math.Pow(mlpx[n].error / mlpx[n].TrainingDataSet.GetLength(0),1/mlpx[n].errExp), 4).ToString();
            if (mlpx[n].classification)
                messageTrainingSetTextBoxN.Text += " a=" + Math.Round(mlpx[n].accuracy / mlpx[n].TrainingDataSet.GetLength(0), 4).ToString();

            //if (CheckBoxDetails1.IsChecked == true)
            //{
            //    System.Diagnostics.Process p = new System.Diagnostics.Process();
            //    p.StartInfo.FileName = "notepad.exe";
            //    p.StartInfo.Arguments = outputFileName + "_trn_e.txt";
            //    p.Start();
            //}
            if (CheckBoxGraphicsN.IsChecked == true && File.Exists(outputFileName + ".png"))
                image1.Source = new BitmapImage(new Uri(outputFileName + ".png"));

            buttonTestN.IsEnabled = true;
            buttonCrossvalidation.IsEnabled = true;
            // Mouse.OverrideCursor = c;

           // messageTrainingSetTextBoxN.Text = ts.TotalMilliseconds.ToString();

        }



        private void NetworkTest(int n, TextBox messageTestSetTextBoxN)
        {
            if (!trainingSetInAnotherFile && Convert.ToDouble(percentTestSetTextBox.Text) < 0.02)
            {
                MessageBox.Show("% test set must be at least 0.02");
                return;
            }
            recentlyLoadedDataSet = false;
            if (trainingSetInAnotherFile)
                mlpx[n].TestDataSet = DataSet;

            if (VSS_version[n] == 0)
                mlpx[n].getError(mlpx[n].TestDataSet, 0, 0, 0, 2, 2, false);
            else 
            {
                mlpx[n].numVectors = mlpx[n].TestDataSet.GetLength(0);
                mlpx[n].SignalTableY = new double[mlpx[n].numVectors, mlpx[n].numLayers, mlpx[n].maxNeurons];
                mlpx[n].SignalTableSumWX = new double[mlpx[n].numVectors, mlpx[n].numLayers, mlpx[n].maxNeurons];
               
                if (VSS_version[n] == 1 || VSS_version[n] == 2)
                    mlpx[n].FillSignalTable(mlpx[n].TestDataSet);
                else
                    mlpx[n].FillSignalTableCross(mlpx[n].TestDataSet);

                mlpx[n].getError_ST(mlpx[n].TestDataSet, 0, 0, 0, Convert.ToDouble(errExpTextBox.Text));
            }



            messageTestSetTextBoxN.Text = "M=" + Math.Round(mlpx[n].error / mlpx[n].TestDataSet.GetLength(0), 4).ToString()
                + " R=" + Math.Round(Math.Pow(mlpx[n].error / mlpx[n].TestDataSet.GetLength(0),1/mlpx[n].errExp), 4).ToString();
            if (mlpx[n].classification)
                messageTestSetTextBoxN.Text += " a=" + Math.Round(mlpx[n].accuracy / mlpx[n].TestDataSet.GetLength(0), 4).ToString();
            //if (CheckBoxDetails1.IsChecked == true)
            //{
            //    System.Diagnostics.Process p = new System.Diagnostics.Process();
            //    p.StartInfo.FileName = "notepad.exe";
            //    p.StartInfo.Arguments = outputFileName + "_tst_e.txt";
            //    p.Start();
            //}
        }


        bool msg = false;
        private void Crossvalidation(object sender, RoutedEventArgs e)
        {
            Cursor c = Mouse.OverrideCursor;
            Mouse.OverrideCursor = Cursors.Wait;
            msg = true;

            recentlyLoadedDataSet = false;
            messageCVTextBox3.Text = "";
            messageCVTextBox.Text = "";
            string outlierReduction = "None";
            if (RadioButtonENN.IsChecked == true)
                outlierReduction = "ENN";
            if (RadioButtonGAS.IsChecked == true)
                outlierReduction = "GAS";

            CV(Convert.ToDouble(TextBoxNoiseValue.Text), Convert.ToDouble(TextBoxNoiseFrequency.Text), Convert.ToDouble(TextBoxNoiseFrequency.Text),
                CheckBoxInputNoise.IsChecked == true, CheckBoxOutputNoise.IsChecked == true,
                Convert.ToDouble(TextBoxTheta.Text), robustAlgorithmComboBox.SelectionBoxItem.ToString(), outlierReduction);
            Mouse.OverrideCursor = c;
        
        }

        string messageCVText3, messageCVText3RMSE, messageCVText3std, messageCVText3RMSEstd, m1, m2;
        StreamWriter swAutoCVMSE, swAutoCVRMSE, swAutoCVACC;
        string sNumLoops = "";
        int currentLoop = 0;

        private void AutoCV(object sender, RoutedEventArgs e)
        {
            messageCVTextBox.Text = "";
            messageCVTextBox2.Text = "";
            messageCVTextBox3.Text = "";
            messageCVTextBox3RMSE.Text = "";
            messageCVText3 = "";
            messageCVText3RMSE = "";
            messageCVText3std = "";
            messageCVText3RMSEstd = "";
            msg = false;
            string cr = "";

            if (ComboBoxTrainingAlgorithm.SelectionBoxItem.ToString().Contains("Cross"))
                cr = "cr";


            if (!recentlyLoadedDataSet)
            {
                Microsoft.Win32.OpenFileDialog dlg1 = new Microsoft.Win32.OpenFileDialog();

                dlg1.InitialDirectory = initDir;
                if (dlg1.ShowDialog() == true)
                {
                    lastInputFileName = dlg1.FileName;
                    LoadTrainingDataSet(lastInputFileName);
                    CheckBoxLastDataFile.Content = "Auto-load " + lastInputFileName;
                }
                else
                    return;
            }


            Cursor c = Mouse.OverrideCursor;
            Mouse.OverrideCursor = Cursors.Wait;
            string outputFileNameAutoCVM = "", outputFileNameAutoCVR = "", outputFileNameAutoCVA = "";

            if (regrRB.IsChecked == true)
            {

                for (int i = 1; i < 10; i++)
                {
                    outputFileNameAutoCVM = directoryName + "\\Results\\" + fileName + "\\AutoCV_" + fileName + "_MSE" + i.ToString() + cr + ".txt";
                    if (!File.Exists(outputFileNameAutoCVR))
                        break;
                }
                for (int i = 1; i < 10; i++)
                {
                    outputFileNameAutoCVR = directoryName + "\\Results\\" + fileName + "\\AutoCV_" + fileName + "_RMSE" + i.ToString() + cr + ".txt";
                    if (!File.Exists(outputFileNameAutoCVR))
                        break;
                }
            }
            else
            {

                for (int i = 1; i < 10; i++)
                {
                    outputFileNameAutoCVA = directoryName + "\\Results\\" + fileName + "\\AutoCV_" + fileName + "_ACC" + i.ToString() + cr + ".txt";
                    if (!File.Exists(outputFileNameAutoCVA))
                        break;
                }
            }



            recentlyLoadedDataSet = false;

            string[] robustAlgorithmName = { "MSE", "ILMedS", "LTA" };
            string[] outlierReductionName = { "None", "ENN", "GAS" };
            double[] vValues = { 0.0, 0.5, 0.85, 1.5, 2.5, 4.0 };
            double[] fValuesIn = { 0.0, 0.20, 0.25, 0.30, 0.35, 0.40 }; //regression          
            double[] fValuesOut = { 0.0, 0.20, 0.25, 0.30, 0.35, 0.40 }; //regression

            if (regrRB.IsChecked == false)
                fValuesOut = new double[] { 0.0, 0.12, 0.24, 0.36, 0.48, 0.60 };   //classification


            double[] theta = { 8.0, 5.5, 4.0, 3.0, 2.0, 1.5 };
            int numLoops = outlierReductionName.Length * robustAlgorithmName.Length * ((vValues.Length - 1) * 3 + 1);
            sNumLoops = numLoops.ToString();
            currentLoop = 0;

            for (int i1 = 0; i1 < outlierReductionName.Length; i1++)
            {
                for (int i2 = 0; i2 < robustAlgorithmName.Length; i2++)
                {

                    if (RadioButtonTwoLineLaTex.IsChecked == true)
                    {
                        messageCVText3 = robustAlgorithmName[i2];
                        messageCVText3RMSE = robustAlgorithmName[i2];
                        messageCVText3std = outlierReductionName[i1];
                        messageCVText3RMSEstd = outlierReductionName[i1];
                    }

                    for (int i = 0; i < vValues.Length; i++)
                        CV(vValues[i], fValuesIn[i], fValuesOut[i], true, false, theta[i], robustAlgorithmName[i2], outlierReductionName[i1]);

                    for (int i = 1; i < vValues.Length; i++)
                        CV(vValues[i], fValuesIn[i], fValuesOut[i], false, true, theta[i], robustAlgorithmName[i2], outlierReductionName[i1]);

                    for (int i = 1; i < vValues.Length; i++)
                        CV(vValues[i], fValuesIn[i], fValuesOut[i], true, true, theta[i], robustAlgorithmName[i2], outlierReductionName[i1]);

                    messageCVTextBox.Text = "";


                    if (RadioButtonTwoLineLaTex.IsChecked == true)
                    {
                        m1 = messageCVText3 + @" \\" + "\r\n" + messageCVText3std + @" \\ \hline";
                        m2 = messageCVText3RMSE + @" \\" + "\r\n" + messageCVText3RMSEstd + @" \\ \hline";
                        //  messageCVTextBox3.Text = m1;
                        //   messageCVTextBox3RMSE.Text = m2;


                        if (!Directory.Exists(directoryName + "\\Results"))
                            Directory.CreateDirectory(directoryName + "\\Results");
                        if (!Directory.Exists(directoryName + "\\Results\\" + fileName))
                            Directory.CreateDirectory(directoryName + "\\Results\\" + fileName);

                        if (regrRB.IsChecked == true)
                        {
                            swAutoCVMSE = new StreamWriter(outputFileNameAutoCVM, true);
                            swAutoCVMSE.Write(m1);
                            swAutoCVMSE.WriteLine();
                            swAutoCVMSE.Close();

                            swAutoCVRMSE = new StreamWriter(outputFileNameAutoCVR, true);
                            swAutoCVRMSE.Write(m2);
                            swAutoCVRMSE.WriteLine();
                            swAutoCVRMSE.Close();
                        }
                        else
                        {
                            swAutoCVACC = new StreamWriter(outputFileNameAutoCVA, true);
                            swAutoCVACC.Write(m1);
                            swAutoCVACC.WriteLine();
                            swAutoCVACC.Close();
                        }

                    }


                }

            }

            if (CheckBoxShowTables.IsChecked == true)
            {
                System.Diagnostics.Process p = new System.Diagnostics.Process();
                p.StartInfo.FileName = "notepad.exe";
                if (regrRB.IsChecked == true)
                    p.StartInfo.Arguments = outputFileNameAutoCVR;
                else
                    p.StartInfo.Arguments = outputFileNameAutoCVA;
                p.Start();
            }


            if (regrRB.IsChecked == true)
                messageCVTextBox.Text = "Results: " + outputFileNameAutoCVR;
            else
                messageCVTextBox.Text = "Results: " + outputFileNameAutoCVA;

            Mouse.OverrideCursor = c;
        }

        private void CV(double v, double fIn, double fOut, bool inputNoise, bool outputNoise, double theta, string robustAlgorithm, string outlierReduction)
        {
            if (Convert.ToInt32(crossvalidationTextBox.Text) > 10 || Convert.ToInt32(crossvalidationRunsTextBox.Text) > 10)
            {
                MessageBox.Show("Fold must be between 2 and 20.");
                return;
            }

                 

         
            //SolidColorBrush scb = (SolidColorBrush)(messageTrainingSetTextBox1.Background);
            //messageCVTextBox.Background = new SolidColorBrush(Colors.Red);
            //messageCVTextBox.Text = "working";

            string resultDirectory = directoryName + "\\Results\\" + fileName + "\\" + DateTime.Now.Ticks.ToString();
            Directory.CreateDirectory(resultDirectory);
            outputFileName = resultDirectory + "\\" + fileName;

            Crossvalidation CV = new Crossvalidation();

            CV.noiseFrequencyIn = fIn; // Convert.ToDouble(TextBoxNoiseFrequency.Text);
            CV.noiseFrequencyOut = fOut; // Convert.ToDouble(TextBoxNoiseFrequency.Text);
            CV.noiseValue = v; // Convert.ToDouble(TextBoxNoiseValue.Text);
            CV.noiseInput = inputNoise; // CheckBoxInputNoise.IsChecked == true;
            CV.noiseOutput = outputNoise; // CheckBoxOutputNoise.IsChecked == true;
            CV.UseExistingCvFiles = CheckBoxUseExistingCvFiles.IsChecked == true;


            string trainingAlgorithm = ComboBoxTrainingAlgorithm.SelectionBoxItem.ToString();
            CV.etaRpropPlus = Convert.ToDouble(TextBoxEtaRpropPlus.Text);
            CV.etaRpropMinus = Convert.ToDouble(TextBoxEtaRpropMinus.Text);
            CV.productUnits = CheckBoxProductUnits.IsChecked == true;
          //  CV.OutlierNoise = Convert.ToDouble(TextBoxOutliers.Text) > 0 && RadioButtonGAS.IsChecked == true;
            double outlierCoef = Convert.ToDouble(TextBoxOutliers.Text);
            if (outlierReduction != "GAS")
            {
                outlierCoef = 0.0;
                lastColumnContainsOutliers = 0;
                CV.OutlierNoise = false;
            }
            else
            {
                lastColumnContainsOutliers = 1;
                CV.OutlierNoise = false;
            }

            CV.SpiltData(DataSet, inputFileName, headerLine, Convert.ToInt32(crossvalidationTextBox.Text));



            int VSS_version = ComboBoxTrainingAlgorithm.SelectedIndex;

            List<int> nHidden = new List<int>();

            if (numHiddenNeurons1TextBox.Text.Trim() != "" && numHiddenNeurons1TextBox.Text.Trim() != "0")
                nHidden.Add(Convert.ToInt32(numHiddenNeurons1TextBox.Text));
            if (numHiddenNeurons2TextBox.Text.Trim() != "" && numHiddenNeurons2TextBox.Text.Trim() != "0")
                nHidden.Add(Convert.ToInt32(numHiddenNeurons2TextBox.Text));
            if (numHiddenNeurons3TextBox.Text.Trim() != "" && numHiddenNeurons3TextBox.Text.Trim() != "0")
                nHidden.Add(Convert.ToInt32(numHiddenNeurons3TextBox.Text));
            if (numHiddenNeurons4TextBox.Text.Trim() != "" && numHiddenNeurons4TextBox.Text.Trim() != "0")
                nHidden.Add(Convert.ToInt32(numHiddenNeurons4TextBox.Text));
            if (numHiddenNeurons5TextBox.Text.Trim() != "" && numHiddenNeurons5TextBox.Text.Trim() != "0")
                nHidden.Add(Convert.ToInt32(numHiddenNeurons5TextBox.Text));

            int[] numHidden = nHidden.ToArray();

            CV.RunCV(trainingAlgorithm, fileName, Convert.ToInt32(numEpochTextBox.Text),
                                          numHidden,
                                          Convert.ToDouble(sumMedianTextBox.Text),
                                          Convert.ToDouble(weightRegularizationTextBox.Text),
                                          outputFileName, transferFunctionComboBox.SelectedIndex,
                                          Convert.ToInt32(crossvalidationTextBox.Text),
                                          Convert.ToDouble(dwTextBox.Text), false, classRB.IsChecked == true,
                                          Convert.ToInt32(crossvalidationRunsTextBox.Text), outlierCoef,
                                          lastColumnContainsOutliers, RadioButtonENN.IsChecked == true, theta, errorMeasureComboBox.SelectedIndex,
                                          Convert.ToDouble(errExpTextBox.Text), robustAlgorithm, VSS_version,
                                          RadioButtonGAS.IsChecked == true);

           // messageCVTextBox.Background = scb;
            if (msg)
            {
                messageCVTextBox.Text = "MSE= " + Math.Round(CV.meanErrorMSE, 4).ToString() + "±" + Math.Round(CV.stdDevErrorMSE, 4).ToString()
                + "  RMSE= " + Math.Round(CV.meanErrorRMSE, 4).ToString() + "±" + Math.Round(CV.stdDevErrorRMSE, 4).ToString();

                if (CV.classification)
                    messageCVTextBox.Text += "  acc=" + Math.Round(CV.meanAccuracy, 4).ToString() + "±" + Math.Round(CV.stdDevAccuracy, 4).ToString();

                if (CV.classification)
                    messageCVTextBox2.Text = Math.Round(CV.meanAccuracy, 2).ToString() + "±" + Math.Round(CV.stdDevAccuracy, 2).ToString();
                else
                {
                    if (CV.meanErrorMSE < 0.01)
                        messageCVTextBox2.Text = "MSE= " + Math.Round(CV.meanErrorMSE, 4).ToString() + "±" + Math.Round(CV.stdDevErrorMSE, 4).ToString()
                        + "  RMSE= " + Math.Round(CV.meanErrorRMSE, 4).ToString() + "±" + Math.Round(CV.stdDevErrorRMSE, 4).ToString();
                    else if (CV.meanErrorMSE < 0.1)
                        messageCVTextBox2.Text = "MSE= " + Math.Round(CV.meanErrorMSE, 3).ToString() + "±" + Math.Round(CV.stdDevErrorMSE, 3).ToString()
                        + "  RMSE= " + Math.Round(CV.meanErrorRMSE, 3).ToString() + "±" + Math.Round(CV.stdDevErrorRMSE, 3).ToString();
                    else
                        messageCVTextBox2.Text = "MSE= " + Math.Round(CV.meanErrorMSE, 2).ToString() + "±" + Math.Round(CV.stdDevErrorMSE, 2).ToString()
                        + "  RMSE= " + Math.Round(CV.meanErrorRMSE, 2).ToString() + "±" + Math.Round(CV.stdDevErrorRMSE, 2).ToString();
                }
            }

            if (RadioButtonOneLineLaTex.IsChecked == true)
            {
                if (CV.classification)
                    messageCVTextBox3.Text += " & " + String.Format("{0:0.00}", CV.meanAccuracy) + @"\scriptsize{$\pm$" + String.Format("{0:0.00}", CV.stdDevAccuracy) + "}";
                else
                {
                    if (CV.meanErrorMSE < 0.01)
                    {
                        messageCVTextBox3.Text += " & " + String.Format("{0:0.0000}", CV.meanErrorMSE) + @"\scriptsize{$\pm$" + String.Format("{0:0.0000", CV.stdDevErrorMSE) + "}";
                        messageCVTextBox3RMSE.Text += " & " + String.Format("{0:0.0000}", CV.meanErrorRMSE) + @"\scriptsize{$\pm$" + String.Format("{0:0.0000}", CV.stdDevErrorRMSE) + "}";
                    }
                    else if (CV.meanErrorMSE < 0.1)
                    {
                        messageCVTextBox3.Text += " & " + String.Format("{0:0.000}", CV.meanErrorMSE) + @"\scriptsize{$\pm$" + String.Format("{0:0.000}", CV.stdDevErrorMSE) + "}";
                        messageCVTextBox3RMSE.Text += " & " + String.Format("{0:0.000}", CV.meanErrorRMSE) + @"\scriptsize{$\pm$" + String.Format("{0:0.000}", CV.stdDevErrorRMSE) + "}";
                    }
                    else
                    {
                        messageCVTextBox3.Text += " & " + String.Format("{0:0.0000}", CV.meanErrorMSE) + @"\scriptsize{$\pm$" + String.Format("{0:0.00}", CV.stdDevErrorMSE) + "}";
                        messageCVTextBox3RMSE.Text += " & " + String.Format("{0:0.0000}", CV.meanErrorRMSE) + @"\scriptsize{$\pm$" + String.Format("{0:0.00}", CV.stdDevErrorRMSE) + "}";
                    }
                }
            }
            else if (RadioButtonTwoLineLaTex.IsChecked == true)
            {
                
                if (CV.classification)
                {
                    messageCVText3 += " & " + String.Format("{0:0.00}", CV.meanAccuracy);
                    messageCVText3std += " & " + String.Format("{0:0.00}", CV.stdDevAccuracy);
                }
                else
                {
                    if (CV.meanErrorMSE < 0.01)
                    {
                        messageCVText3 += " & " + String.Format("{0:0.0000}", CV.meanErrorMSE);
                        messageCVText3std += " & " + String.Format("{0:0.0000}", CV.stdDevErrorMSE);
                        messageCVText3RMSE += " & " + String.Format("{0:0.0000}", CV.meanErrorRMSE);
                        messageCVText3RMSEstd = " & " + String.Format("{0:0.0000}", CV.stdDevErrorRMSE);
                    }
                    else if (CV.meanErrorMSE < 0.1)
                    {
                        messageCVText3 += " & " + String.Format("{0:0.000}", CV.meanErrorMSE);
                        messageCVText3std += " & " + String.Format("{0:0.000}", CV.stdDevErrorMSE);
                        messageCVText3RMSE += " & " + String.Format("{0:0.000}", CV.meanErrorRMSE);
                        messageCVText3RMSEstd += " & " + String.Format("{0:0.000}", CV.stdDevErrorRMSE);
                    }
                    else
                    {
                        messageCVText3 += " & " + String.Format("{0:0.00}", CV.meanErrorMSE);
                        messageCVText3std += " & " + String.Format("{0:0.00}", CV.stdDevErrorMSE);
                        messageCVText3RMSE += " & " + String.Format("{0:0.00}", CV.meanErrorRMSE);
                        messageCVText3RMSEstd += " & " + String.Format("{0:0.00}", CV.stdDevErrorRMSE);
                    }
                }
            }
            string sL = (++currentLoop).ToString() + "/" + sNumLoops;
            messageCVTextBox2.Text = sL;
            messageCVTextBox2.Dispatcher.Invoke(new Action(() => this.messageCVTextBox2.Text = sL), System.Windows.Threading.DispatcherPriority.Input);
            System.Threading.Thread.Sleep(1000 * Convert.ToInt32(TextBoxCoolTime.Text));
            
        }






        private void TanhXYregression(object sender, RoutedEventArgs e)
        {
            Microsoft.Win32.OpenFileDialog dlg1 = new Microsoft.Win32.OpenFileDialog();
            if (dlg1.ShowDialog() == true)
                Utils.Tanh(dlg1.FileName, 2);
        }

        private void TanhXclassification(object sender, RoutedEventArgs e)
        {
            Microsoft.Win32.OpenFileDialog dlg1 = new Microsoft.Win32.OpenFileDialog();
            if (dlg1.ShowDialog() == true)
                Utils.Tanh(dlg1.FileName, 0);
        }


        private void TanhXregression(object sender, RoutedEventArgs e)
        {
            Microsoft.Win32.OpenFileDialog dlg1 = new Microsoft.Win32.OpenFileDialog();
            if (dlg1.ShowDialog() == true)
                Utils.Tanh(dlg1.FileName, 1);
        }

        private void Shuffle(object sender, RoutedEventArgs e)
        {
            Microsoft.Win32.OpenFileDialog dlg1 = new Microsoft.Win32.OpenFileDialog();
            if (dlg1.ShowDialog() == true)
                Utils.Shuffle(dlg1.FileName);
        }

        private void Outliers(object sender, RoutedEventArgs e)
        {
            Microsoft.Win32.OpenFileDialog dlg1 = new Microsoft.Win32.OpenFileDialog();
            if (dlg1.ShowDialog() == true)
            {
                kNN knn = new kNN(dlg1.FileName, Convert.ToInt32(TextBoxkNN.Text), 2);
                knn.GetDistances();
                knn.ENN();

                string fileName3 = dlg1.FileName.Substring(0, dlg1.FileName.LastIndexOf("."));
                string outputFile3 = fileName3 + "_OUTL.txt";
                knn.SaveDataSetWithOutliers(0, ref outputFile3);


            }
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Crossvalidation CV = new Crossvalidation();   

            CV.noiseFrequencyIn = Convert.ToDouble(TextBoxNoiseFrequency.Text);
            CV.noiseFrequencyOut = CV.noiseFrequencyIn;
            CV.noiseValue = Convert.ToDouble(TextBoxNoiseValue.Text);
            CV.noiseInput = CheckBoxInputNoise.IsChecked == true;
            CV.noiseOutput = CheckBoxOutputNoise.IsChecked == true;

            int d = lastInputFileName.LastIndexOf("."); 
            string noisyFileName = lastInputFileName.Substring(0, d)
                + "_v" + TextBoxNoiseValue.Text.Replace(".", "") + "_f" + TextBoxNoiseFrequency.Text.Replace(".", "") + ".txt";
            CV.AddNoise(DataSet, noisyFileName, headerLine);
        }

        private void CheckBoxLastDataFile_Checked(object sender, RoutedEventArgs e)
        {
            buttonTraining1.IsEnabled = true;
            buttonTraining2.IsEnabled = true;
            buttonTraining3.IsEnabled = true;
            buttonTraining4.IsEnabled = true;
            ContrastiveDivergenceButton.IsEnabled = true;
            ContrastiveDivergenceDiscrButton.IsEnabled = true;
            buttonCrossvalidation.IsEnabled = true;
            buttonAutoCV.IsEnabled = true;
        }

        private void LoadConfiguration_Click(object sender, RoutedEventArgs e)
        {
           
            Microsoft.Win32.OpenFileDialog dlg2 = new Microsoft.Win32.OpenFileDialog();
            dlg2.InitialDirectory = initDir;

            if (dlg2.ShowDialog() == true)
            {
                LoadConfiguration(dlg2.FileName);
            }
        }

        private void LoadWeights_Click(object sender, RoutedEventArgs e)
        {
            Microsoft.Win32.OpenFileDialog dlg3 = new Microsoft.Win32.OpenFileDialog();
            dlg3.InitialDirectory = initDir;

            if (dlg3.ShowDialog() == true)
            {
                File.Copy(dlg3.FileName, "weights.txt", true);
                CheckBoxRandomWeights.IsChecked = false;
            }
        }

        private void ContrastiveDivergence_Click(object sender, RoutedEventArgs e)
        {
            if (CheckBoxLastDataFile.IsChecked == true)
                LoadDataByFileName(lastInputFileName);           
                ContrastiveDivergence CD = new ContrastiveDivergence(DataSet,"C");
                messageCVTextBox3.Text = CD.errorString;
        }

        private void ContrastiveDivergenceDiscr_Click(object sender, RoutedEventArgs e)
        {
            if (CheckBoxLastDataFile.IsChecked == true)
                LoadDataByFileName(lastInputFileName);
            ContrastiveDivergence CD = new ContrastiveDivergence(DataSet,"D");
            messageCVTextBox3.Text = CD.errorString;
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            Microsoft.Win32.OpenFileDialog dlg3 = new Microsoft.Win32.OpenFileDialog();
            dlg3.InitialDirectory = initDir;

            if (dlg3.ShowDialog() == true)         
                Utils.Transpose(dlg3.FileName);
            
        }


    }
}
